$(function () {
  var layer = layui.layer;
  var form = layui.form;
  var laypage = layui.laypage;
  //----------------------------------------------------------------
  //  页面查询参数
  var query = {
    pagenum: 1, //页码值
    pagesize: 5, //每页显示几条数据
    lab_class_name: "", //宿舍名称
    lab_class_person: "", //宿舍负责人
    lab_status: "", //宿舍预约状态
  };
  // 渲染分页功能
  function renderPage(total) {
    // console.log(total);
    laypage.render({
      elem: "pageBox", //分页容器的id
      count: total, //数据总数，从服务端得到
      limit: query.pagesize, //每页显示几条数据
      curr: query.pagenum,
      layout: ["count", "limit", "prev", "page", "next", "skip"],
      limits: [5, 10, 15, 20],
      jump: function (obj, first) {
        // console.log(first);
        query.pagenum = obj.curr;
        query.pagesize = obj.limit;
        // 首行不执行
        if (!first) {
          initLabList();
        }
      },
    });
  }
  //------------------------------------------------------------------------------
  // 调用渲染宿舍列表信息的函数
  initLabList();
  //------------------------------------------------------------------------------
  // 获取所有宿舍的列表
  function initLabList() {
    var lab_class_name = $("#form_search [name = lab_class_name ]").val();
    var lab_address = $("#form_search [name = lab_address]").val();
    var status = $("#form_search [name = status]").val();
    // console.log(typeof (status));
    // console.log(lab_class_name);
    // console.log(lab_address);
    query.lab_class_name = lab_class_name;
    query.lab_address = lab_address;
    query.lab_status = status;
    $.ajax({
      method: "GET",
      url: "/my/labManage/labList/",
      data: query,
      success: function (res) {
        // console.log(res);
        var htmStr = template("tpl_lab_list", res);
        $("tbody").html(htmStr);
        renderPage(res.total);
      },
    });
  }
  //----------------------------------------------------------------
  //模糊搜索需要的信息
  $("#form_search").on("submit", function (e) {
    e.preventDefault();
    initLabList();
  });
  //----------------------------------------------------------------
  // 宿舍
  $("body").on("click", ".btn_appointment", function () {
    var id = $(this).attr("data_id");
    // console.log(id);
    // console.log('ok');
    $.ajax({
      method: "GET",
      url: "/my/labManage/labAppointment/" + id,
      success: function (res) {
        if (res.status !== 0) {
          return layer.msg(res.message);
        }
        initLabList();
        layer.msg("预约宿舍成功！");
      },
    });
  });
  //----------------------------------------------------------------
  // 归还
  $("body").on("click", ".btn_cancel", function () {
    var id = $(this).attr("data_id");
    // console.log(id);
    // console.log('ok');
    $.ajax({
      method: "GET",
      url: "/my/labManage/labCancel/" + id,
      success: function (res) {
        if (res.status !== 0) {
          return layer.msg(res.message);
        }
        initLabList();
        layer.msg("归还宿舍成功！");
      },
    });
  });
});
